﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using HosPlat.Data;

namespace HosPlat.API
{
    /// <summary>
    /// API 的摘要说明
    /// </summary>
    public class API : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            string strAction = context.Request["action"] ?? "";
            string strpageindex = context.Request["pageindex"] ?? "";
            string strpagecount = context.Request["pagecount"] ?? "";

            string P1 = context.Request["P1"] ?? "";
            string P2 = context.Request["P2"] ?? "";
            string P3 = context.Request["P3"] ?? "";
            Msg_Result msg = new Msg_Result() { action = strAction.ToUpper(), error = "" };
            if (!string.IsNullOrEmpty(strAction))
            {
                try
                {
                    Msg_Page page = new Msg_Page();
                    if (!string.IsNullOrEmpty(strpageindex))
                    {
                        int pindex = 0;
                        Int32.TryParse(strpageindex, out pindex);
                        page.pageindex = pindex;
                    }
                    if (!string.IsNullOrEmpty(strpagecount))
                    {
                        int pcount = 0;
                        Int32.TryParse(strpagecount, out pcount);
                        page.pagecount = pcount;
                    }

                    #region 接口调用

                    if (msg.action.IndexOf("_") <= 0)
                    {
                        msg.error = "接口错误";
                    }
                    else
                    {
                        string action1 = msg.action.Split('_')[0];
                        HT_Auth_User authuser = new HT_Auth_User();
                        if (msg.action == "BASE_LOGIN" || msg.action == "BASE_AUTO")
                        {
                            var container = ServiceContainer.Current().Resolve<IWsService>(action1);
                            container.ProcessRequest(context, ref msg, page, authuser, P1.TrimEnd(), P2.TrimEnd());
                        }
                        else
                        {
                            if (context.Request.Cookies["wxyycode"] != null && context.Request.Cookies["wxyycode"].Value != "")//如果存在TOKEN,根据TOKEN找到用户信息，并根据权限执行具体ACTION
                            {
                                string code = context.Request.Cookies["wxyycode"].Value.ToString();
                                authuser = new HT_Auth_UserB().GetEntity(p => p.Code == code);
                                if (authuser != null)
                                {
                                    var container = ServiceContainer.Current().Resolve<IWsService>(action1);
                                    container.ProcessRequest(context, ref msg, page, authuser, P1.TrimEnd(), P2.TrimEnd());
                                }
                            }
                            else
                            {
                                msg.error = "登录超时，请重新登录";
                            }
                        }



                    }



                    #endregion

                }
                catch (Exception ex)
                {
                    msg.error = "操作异常，请重试" + ex.Message;

                }
            }

            string jsonpcallback = context.Request["jsonpcallback"] ?? "";

            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string Result = JsonConvert.SerializeObject(msg, Formatting.Indented, timeConverter).Replace("null", "\"\"");
            if (jsonpcallback != "")
            {
                Result = jsonpcallback + "(" + Result + ")";//JSONP支持跨域
            }
            context.Response.Write(Result);

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

